package zengjin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.PageResponse;
import zengjin.mapper.EmployerMapper;
import zengjin.mapper.ImageMapper;
import zengjin.model.dto.employer.PageEmployerRequest;
import zengjin.model.dto.employer.UpdateEmployerPersonalDto;
import zengjin.model.en.Employer;
import zengjin.model.en.Image;
import zengjin.model.vo.employer.EmployerVO;
import zengjin.service.EmployerService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【employer】的数据库操作Service实现
 * @createDate 2025-01-02 14:22:19
 */
@Service
public class EmployerServiceImpl extends ServiceImpl<EmployerMapper, Employer>
        implements EmployerService {

    @Resource
    EmployerMapper employerMapper;

    @Resource
    private ImageMapper imageMapper;

    @Override
    public boolean updateEmployerPersonal(UpdateEmployerPersonalDto employerPersonalDto) {
        //1.获取参数
        Long employerId = employerPersonalDto.getEmployerId();
        String employerUsername = employerPersonalDto.getEmployerUsername();
        String employerPhone = employerPersonalDto.getEmployerPhone();
        String employerName = employerPersonalDto.getEmployerName();
        String employerCompanyName = employerPersonalDto.getEmployerCompanyName();
        String employerCompanyIndustry = employerPersonalDto.getEmployerCompanyIndustry();
        String employerCompanyAddress = employerPersonalDto.getEmployerCompanyAddress();
        String employerCompanyDescription = employerPersonalDto.getEmployerCompanyDescription();
        String employerCompanyWebsite = employerPersonalDto.getEmployerCompanyWebsite();
        String userAvatarUrl = employerPersonalDto.getUserAvatarUrl();

        //2.更新头像信息
        int update = 0;
        if (StrUtil.isNotBlank(userAvatarUrl)) {
            LambdaUpdateWrapper<Image> updateImgWrapper = new LambdaUpdateWrapper<>();
            updateImgWrapper.eq(Image::getImageNameId, employerId)
                    .eq(Image::getImageName, "employer_avatar")
                    .set(StrUtil.isNotBlank(userAvatarUrl), Image::getImageUrl, userAvatarUrl);
            update = imageMapper.update(null, updateImgWrapper);
        }

        //3.更新雇主信息
        LambdaUpdateWrapper<Employer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Employer::getEmployerId, employerId)
                .set(StrUtil.isNotBlank(employerUsername), Employer::getEmployerUsername, employerUsername)
                .set(StrUtil.isNotBlank(employerPhone), Employer::getEmployerPhone, employerPhone)
                .set(StrUtil.isNotBlank(employerName), Employer::getEmployerName, employerName)
                .set(StrUtil.isNotBlank(employerCompanyName), Employer::getEmployerCompanyName, employerCompanyName)
                .set(StrUtil.isNotBlank(employerCompanyIndustry), Employer::getEmployerCompanyIndustry, employerCompanyIndustry)
                .set(StrUtil.isNotBlank(employerCompanyAddress), Employer::getEmployerCompanyAddress, employerCompanyAddress)
                .set(StrUtil.isNotBlank(employerCompanyDescription), Employer::getEmployerCompanyDescription, employerCompanyDescription)
                .set(StrUtil.isNotBlank(employerCompanyWebsite), Employer::getEmployerCompanyWebsite, employerCompanyWebsite);
        return (employerMapper.update(null, updateWrapper) + update) > 0;

    }

    @Override
    public PageResponse<EmployerVO> getEmployerAdminPage(PageEmployerRequest pageEmployerRequest) {
        //1.获取参数
        String searchName = pageEmployerRequest.getSearchName();
        String employerIsVerified = pageEmployerRequest.getEmployerIsVerified();
        String employerStatus = pageEmployerRequest.getEmployerStatus();

        //2.构建分页条件
        Page<Employer> page = pageEmployerRequest.toPageDefaultSortByUpdateTime();

        //3.查询出集合
        QueryWrapper<Employer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(employerIsVerified), "employer_is_verified", employerIsVerified)
                .eq(StrUtil.isNotBlank(employerStatus), "employer_status", employerStatus)
                .and(StrUtil.isNotBlank(searchName),
                        wrapper -> wrapper.like("employer_company_name", searchName)
                                .or()
                                .like("employer_company_address", searchName))
                .orderByDesc("update_time");
        List<Employer> employers = employerMapper.selectList(page, queryWrapper);
        List<EmployerVO> employerVOS = new ArrayList<>();
        employers.forEach(employer -> {
            EmployerVO employerVO = BeanUtil.copyProperties(employer, EmployerVO.class);
            //查询头像
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "employer_avatar");
            imageQueryWrapper.eq("image_name_id", employer.getEmployerId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                employerVO.setUserAvatarUrl(image.getImageUrl());
            }
            employerVOS.add(employerVO);
        });
        return PageResponse.of(page, employerVOS);


    }

    @Override
    public boolean updateEmployerVerified(Long employerId, String employerIsVerified) {
        //1.构造条件
        LambdaUpdateWrapper<Employer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Employer::getEmployerId, employerId)
                .set(Employer::getEmployerIsVerified, employerIsVerified);
        //2.更新
        return employerMapper.update(null, updateWrapper) > 0;
    }
}




